#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
检查数据库表结构
"""

import pymysql

DB_CONFIG = {
    'host': 'localhost',
    'port': 3306,
    'user': 'flask_dev',
    'password': 'dev123456',
    'database': 'flask_local',
    'charset': 'utf8mb4'
}

def check_tables():
    """检查数据库中的表"""
    try:
        connection = pymysql.connect(**DB_CONFIG)
        with connection.cursor() as cursor:
            # 获取所有表
            cursor.execute("SHOW TABLES")
            tables = cursor.fetchall()
            
            print("\n" + "="*50)
            print("[+] 数据库表列表")
            print("="*50)
            
            for table in tables:
                table_name = table[0]
                
                # 获取表的记录数
                cursor.execute(f"SELECT COUNT(*) FROM {table_name}")
                count = cursor.fetchone()[0]
                
                print(f"[OK] {table_name:<30} {count:>6} 条记录")
            
            print("="*50 + "\n")
            
            # 检查评估相关的表
            print("检查评估相关的表：")
            assessment_tables = [
                'scale_assessment',
                'scale_hamd', 
                'scale_hama',
                'scale_ymrs',
                'hamd_detail',
                'hama_detail', 
                'ymrs_detail'
            ]
            
            for table in assessment_tables:
                try:
                    cursor.execute(f"SELECT COUNT(*) FROM {table}")
                    count = cursor.fetchone()[0]
                    print(f"  [OK] {table} 存在，{count} 条记录")
                except pymysql.err.ProgrammingError:
                    print(f"  [X] {table} 不存在")
        
        connection.close()
        
    except Exception as e:
        print(f"[ERROR] {e}")

if __name__ == '__main__':
    check_tables()

